use "$project_path/data/4_regdata/regdata", clear

keep if runvar_alt!=. & treatment_alt!=.

log using "$project_path/output/tables/Table_3.smcl", replace

// Column 1
rdrobust diff_m1_chix_econ runvar_alt, fuzzy(treatment_alt) all
rdrobust diff_m1_chix_econ runvar_alt, all
reg diff_m1_chix_econ treatment_alt

// Column 2
rdrobust std_diff_m1_growth_gdpc_penn runvar_alt, fuzzy(treatment_alt) all
rdrobust std_diff_m1_growth_gdpc_penn runvar_alt, all
reg std_diff_m1_growth_gdpc_penn treatment_alt

// Column 3
rdrobust mstd_diff_m1_inflation_imf runvar_alt, fuzzy(treatment_alt) all
rdrobust mstd_diff_m1_inflation_imf runvar_alt, all
reg mstd_diff_m1_inflation_imf treatment_alt

// Column 4
rdrobust mstd_diff_m1_unemployment_ilo runvar_alt, fuzzy(treatment_alt) all
rdrobust mstd_diff_m1_unemployment_ilo runvar_alt, all
reg mstd_diff_m1_unemployment_ilo treatment_alt

// Column 5
rdrobust std_diff_m1_trade_wb runvar_alt, fuzzy(treatment_alt) all
rdrobust std_diff_m1_trade_wb runvar_alt, all
reg std_diff_m1_trade_wb treatment_alt

// Column 6
rdrobust std_diff_m1_hdi runvar_alt, fuzzy(treatment_alt) all
rdrobust std_diff_m1_hdi runvar_alt, all
reg std_diff_m1_hdi treatment_alt

// Column 7
rdrobust std_diff_m1_democracy_index runvar_alt, fuzzy(treatment_alt) all
rdrobust std_diff_m1_democracy_index runvar_alt, all
reg std_diff_m1_democracy_index treatment_alt

// Column 8
rdrobust diff_m1_general_chix runvar_alt, fuzzy(treatment_alt) all 
rdrobust diff_m1_general_chix runvar_alt, all
reg diff_m1_general_chix treatment_alt

log close

//Store estimates
local outcomes diff_m1_chix_econ ///
			   std_diff_m1_growth_gdpc_penn ///
			   mstd_diff_m1_inflation_imf ///
			   mstd_diff_m1_unemployment_ilo ///
			   std_diff_m1_trade_wb ///
			   std_diff_m1_hdi ///
			   std_diff_m1_democracy_index ///
			   diff_m1_general_chix

// Initialize matrix of results and define value labels
local n_estimates = 22
local n_outcomes = 8
local ncols = `n_estimates' + 1
local nrows = `n_outcomes'

//Prepare matrix of coefficients and other statistics
mat Coefs = J(`nrows', `ncols', .)
			  
local count_outcome = 0			  
foreach v in `outcomes'{
	
	local col = 2
	local ++count_outcome
	
	rdrobust `v' runvar_alt, all
	
	//Store estimates and stats from the sharp RD form
	local sharp_b_`count_outcome': di %5.3f  e(tau_cl)
	local sharp_se_`count_outcome': di %5.3f e(se_tau_rb)
	local sharp_p_`count_outcome': di %5.4f e(pv_rb)
	local sharp_N_`count_outcome': di %6.0f e(N)
	loca sharp_N_eff_`count_outcome': di %6.0f e(N_h_l) + e(N_h_r)
	local sharp_bw_`count_outcome': di %5.1f e(h_r)	


	mat Coefs[`count_outcome',`col'] = `sharp_b_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `sharp_se_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `sharp_p_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `sharp_N_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `sharp_N_eff_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `sharp_bw_`count_outcome''
	local ++col
	
	rdrobust `v' runvar_alt, fuzzy(treatment_alt) all
	
	//Store estimates and stats from the fuzzy estimate RD
	local fuzzy_b_`count_outcome': di %5.3f  e(tau_cl)
	local fuzzy_se_`count_outcome': di %5.3f e(se_tau_rb)
	local fuzzy_p_`count_outcome': di %5.4f e(pv_rb)
	local fuzzy_N_`count_outcome': di %6.0f e(N)
	loca fuzzy_N_eff_`count_outcome': di %6.0f e(N_h_l) + e(N_h_r)
	local fuzzy_bw_`count_outcome': di %5.1f e(h_r)	
	
	mat Coefs[`count_outcome',`col'] = `fuzzy_b_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `fuzzy_se_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `fuzzy_p_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `fuzzy_N_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `fuzzy_N_eff_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `fuzzy_bw_`count_outcome''
	local ++col

	//Store estimates and stats from the first-stage
	local FS_b_`count_outcome': di %5.3f  e(tau_T_cl)
	local FS_se_`count_outcome': di %5.3f e(se_tau_T_rb)
	local FS_N_`count_outcome': di %6.0f e(N)
	loca FS_N_eff_`count_outcome': di %6.0f e(N_h_l) + e(N_h_r)
	local FS_bw_`count_outcome': di %5.1f e(h_r)
	//Store the p-value of the first-stage
	rdrobust treatment_alt runvar_alt if `v'!=., c(0) all
	local FS_p_`count_outcome': di %5.4f e(pv_rb)
	
	mat Coefs[`count_outcome',`col'] = `FS_b_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `FS_se_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `FS_p_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `FS_N_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `FS_N_eff_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `FS_bw_`count_outcome''
	local ++col

	reg `v' treatment_alt
	
	//Store estimates for the OLS regression
	local ols_b_`count_outcome': di %5.3f  r(table)["b", "treatment_alt"]
	local ols_se_`count_outcome': di %5.3f r(table)["se", "treatment_alt"]
	local ols_p_`count_outcome': di %5.4f r(table)["pvalue", "treatment_alt"]
	local ols_N_`count_outcome': di %6.0f e(N)
	
	mat Coefs[`count_outcome',`col'] = `ols_b_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `ols_se_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `ols_p_`count_outcome''
	local ++col
	mat Coefs[`count_outcome',`col'] = `ols_N_`count_outcome''
	local ++col
		
}
mat list Coefs

********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
// Clear the dataset, and use the matrix as dataset			

clear
svmat Coefs
local varnames varname sharp_tau sharp_SE sharp_pval sharp_N sharp_N_eff ///
	           sharp_bw  fuzzy_tau fuzzy_SE fuzzy_pval fuzzy_N ///
			   fuzzy_N_eff fuzzy_bw first_stage_tau ///
			   first_stage_SE first_stage_pval first_stage_N first_stage_N_eff ///
			   first_stage_bw ols_tau ols_SE ols_pval ols_N
			   
local i = 1
foreach v in `varnames'{

	rename Coefs`i' `v'
	local ++i
		
}

gen temp = _n
tostring varname, replace

local outcomes chix_econ			///
			   growth_gdpc_penn		///
			   inflation_imf		///
			   unemployment_ilo		///
			   trade_wb				///
			   chix_social			///
			   chix_democracy		///
			   general_chix

local i = 1
foreach o in `outcomes'{

	replace varname = "`o'" if temp == `i'
	local ++i
		
}
drop temp



foreach v in sharp_N sharp_N_eff fuzzy_N fuzzy_N_eff first_stage_N first_stage_N_eff ols_N {
	tostring `v', replace force
}
foreach v in sharp_bw fuzzy_bw first_stage_bw {
	recast double `v'
	replace `v' = round(`v', 0.1)
	tostring `v', replace force
	replace `v' = `v'+"00" if strpos(`v',".")!=0
	replace `v' = `v'+".00" if strpos(`v',".")==0
	replace `v' = substr(`v', 1, 4)
}

foreach prefix in sharp_ fuzzy_ first_stage_ ols_ {
	gen temp_`prefix'tau = `prefix'tau
	gen temp_`prefix'pval = `prefix'pval
	foreach est_var in tau SE pval {
		recast double `prefix'`est_var'
		replace `prefix'`est_var' = round(`prefix'`est_var', 0.001)
		tostring `prefix'`est_var', replace force
		replace `prefix'`est_var' = `prefix'`est_var' + "000"
	}
	replace `prefix'tau = substr(`prefix'tau,1,4) if temp_`prefix'tau>0
	replace `prefix'tau = substr(`prefix'tau,2,4) if temp_`prefix'tau<=0
	replace `prefix'tau = `prefix'tau+"*" if temp_`prefix'pval<0.1
	replace `prefix'tau = `prefix'tau+"*" if temp_`prefix'pval<0.05
	replace `prefix'tau = `prefix'tau+"*" if temp_`prefix'pval<0.01
	replace `prefix'SE = substr(`prefix'SE,1,4)
	replace `prefix'SE = "(0"+`prefix'SE+")"
	replace `prefix'tau = "0"+`prefix'tau if temp_`prefix'tau>0
	replace `prefix'tau = "-0"+`prefix'tau if temp_`prefix'tau<0
	replace `prefix'pval = substr(`prefix'pval,1,4)
	replace `prefix'pval = "0"+`prefix'pval if `prefix'pval!="1"
	replace `prefix'pval = "$<$0.001" if temp_`prefix'pval<0.001
	replace `prefix'pval = "["+`prefix'pval+"]"
	//Remove 0s in excess at the beginning of coefficients
	forvalues i=1/9 {
		replace `prefix'tau = subinstr(`prefix'tau,"0`i'.","`i'.",.) if substr(`prefix'tau, 1, 3)=="0`i'."
		replace `prefix'tau = subinstr(`prefix'tau,"-0`i'.","-`i'.",.) if substr(`prefix'tau, 1, 4)=="-0`i'."
	}
}
drop temp*


order varname fuzzy_tau fuzzy_SE fuzzy_pval fuzzy_N fuzzy_N_eff fuzzy_bw sharp_tau sharp_SE sharp_pval sharp_N sharp_N_eff sharp_bw  first_stage_tau first_stage_SE first_stage_pval first_stage_N first_stage_N_eff first_stage_bw ols_tau ols_SE ols_pval ols_N

sxpose, clear firstnames

gen temp = _n

gen description = ""
replace description = "\multicolumn{9}{l}{\textbf{Panel a: Fuzzy RDD (Effects of an executive turnover)}} \\ \hline Ex. turn." if temp==1
replace description = "" if temp==2
replace description = "p-val." if temp==3
replace description = "\hline N" if temp==4
replace description = "N eff." if temp==5
replace description = "Band." if temp==6
replace description = "\hline \multicolumn{9}{l}{\textbf{Panel b: Reduced form (Effects of a defeat of the leading party before the election)}} \\ \hline El. defeat" if temp==7
replace description = "" if temp==8
replace description = "p-val." if temp==9
replace description = "\hline N" if temp==10
replace description = "N eff." if temp==11
replace description = "Band." if temp==12
replace description = "\hline \multicolumn{9}{l}{\textbf{Panel c: First stage (Effects of a defeat of the leading party before the election on the probability of an}} \\ \multicolumn{9}{l}{\textbf{executive turnover)}} \\ \hline El. defeat" if temp==13
replace description = "" if temp==14
replace description = "p-val." if temp==15
replace description = "\hline N" if temp==16
replace description = "N eff." if temp==17
replace description = "Band." if temp==18
replace description = "\hline \multicolumn{9}{l}{\textbf{Panel d: OLS (Effects of an executive turnover)}} \\  \hline Ex. turn." if temp==19
replace description = "" if temp==20
replace description = "p-val." if temp==21
replace description = "\hline N" if temp==22

lab var description ""
lab var chix_econ "\textbf{Econ. perf.}"
lab var growth_gdpc_penn "GDP p.c. gr."
lab var inflation_imf "(Minus) Inflation"
lab var unemployment_ilo "(Minus) Unemp."
lab var trade_wb "Trade"
lab var chix_social "\textbf{HDI}"
lab var chix_democracy "\textbf{Democ.}"
lab var general_chix "\textbf{General index}"

drop temp

order description, first

local title "Effects of turnovers in the executive branch on country performance"
local label_key "tab:alternative_sharp_fuzzy"
local output "$project_path/output/tables/Table_3.tex"
local footnote = "\caption*{\footnotesize \emph{Notes}: This table reports estimated effects of turnovers in the executive branch for the sample of elections leading to the appointment of a leader in the executive branch. In panel a, we report fuzzy RDD estimates of the effects of executive turnovers, using as assignment variable the defeat of the leading party before the election and turnover in the executive branch as treatment. We show estimates of $\gamma$ in equation (\ref{eq:alternative_rdd}) -- see Appendix \ref{app:executive_turnover}. In panel b, we report reduced form estimates of $\gamma^r$ in equation (\ref{eq:alternative_rdd_rf}) -- see Appendix \ref{app:executive_turnover}, corresponding to the effects of an electoral defeat of the leading party before the election. We use the margin of victory of the best ranked challenger of the leading party before the election as the running variable. In panel c, we report estimates of $\tilde \gamma$ in equation (\ref{eq:alternative_rdd_fs}), corresponding to the first stage of panel a. In panel d, we report OLS estimates of the effect of executive turnovers. Details about the definition of the leading party before the election can be found in Section \ref{sec:turnoverAlt}. $^{*} p<0.10,^{**} p<0.05,^{***} p<0.01$.}"

texsave using "`output'", replace title("`title'") varlabels footnote("`footnote'") frag nofix marker("`label_key'") headerlines("& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8)") location(h)

